package com.wc.alorithm_blue_bridge._DFS.五子棋对弈;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/3/10 9:57
 * @description
 * https://www.lanqiao.cn/courses/46292/learning/?id=3441381&compatibility=false
 */
public class Main {
    /**
     * 思路：
     * 细心
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 30, M = 25;
    static int[][] g = new int[N][N];
    static int n, res = 0;

    public static void main(String[] args) {
        n = 5;
        dfs(12, 0);
        out.println(res);
        out.flush();
    }


    static void dfs(int u, int s) {
        if (u == 0) {
            if (check()) res++;
            return;
        }
        if (s >= n * n) return;
        int x = s / n, y = s % n;
        g[x][y] = 1;
        dfs(u - 1, s + 1);
        g[x][y] = 0;
        dfs(u, s + 1);
    }

    static boolean check() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int x = g[i][j];
                int sum = 0;
                // 同一列
                for (int k = i; k < i + 5; k++) {
                    if (k < n && g[k][j] == x) sum++;
                }
                if (sum >= 5) return false;

                sum = 0;
                // 同一行
                for (int k = j; k < j + 5; k++) {
                    if (k < n && g[i][k] == x) sum++;
                }
                if (sum >= 5) return false;

                sum = 0;
                // 斜着45度
                for (int k = 0; k < 5; k++) {
                    int a = i + k, b = j + k;
                    if (a < n && b < n && g[a][b] == x) sum++;

                }
                if (sum >= 5) return false;

                sum = 0;
                for (int k = 0; k < 5; k++) {
                    int a = i + k, b = j - k;
                    if (a < n && b >= 0 && g[a][b] == x) sum++;
                }
                if (sum >= 5) return false;
            }
        }
        return true;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

